﻿using HelixToolkit.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Learn3D.Helix
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // Create a model group
            Model3DGroup modelGroup = new Model3DGroup();

            MeshBuilder meshBoxBuilder = new MeshBuilder(false, false);
            meshBoxBuilder.AddBox(new Point3D(0, 0, 0), 40, 40, 40);
            MeshGeometry3D meshBox = meshBoxBuilder.ToMesh(true);
            var whiteMaterial = MaterialHelper.CreateMaterial(Colors.Green);
            var insideMaterial = MaterialHelper.CreateMaterial(Colors.Gray);
            modelGroup.Children.Add(new GeometryModel3D { Geometry = meshBox, Material = whiteMaterial, BackMaterial = insideMaterial });

            this.model.Content = modelGroup;
        }

        private void Load3dsModel()
        {
            string ModelsdsFile = @"E:\Study\Blogs\learn-wpf\Learn3D\Model\100100.3DS";
            Model3DGroup models = LoadModel(ModelsdsFile);


            foreach (Model3D model in models.Children)
            {
                if (model is GeometryModel3D)
                {
                    GeometryModel3D body = model as GeometryModel3D;
                    body.Material = MaterialHelper.CreateMaterial(Colors.Green);
                    body.BackMaterial = MaterialHelper.CreateMaterial(Colors.Red);
                }
            }

            model.Content = models;
        }

        public static Model3DGroup LoadModel(string path)
        {
            if (path == null)
            {
                return null;
            }

            Model3DGroup model = null;
            string ext = System.IO.Path.GetExtension(path).ToLower();
            switch (ext)
            {
                case ".3ds":
                    {
                        var r = new HelixToolkit.Wpf.StudioReader();
                        model = r.Read(path);
                        break;
                    }

                case ".lwo":
                    {
                        var r = new HelixToolkit.Wpf.LwoReader();
                        model = r.Read(path);

                        break;
                    }

                case ".obj":
                    {
                        var r = new HelixToolkit.Wpf.ObjReader();
                        model = r.Read(path);
                        break;
                    }

                case ".objz":
                    {
                        var r = new HelixToolkit.Wpf.ObjReader();
                        model = r.ReadZ(path);
                        break;
                    }

                case ".stl":
                    {
                        var r = new HelixToolkit.Wpf.StLReader();
                        model = r.Read(path);
                        break;
                    }

                case ".off":
                    {
                        var r = new HelixToolkit.Wpf.OffReader();
                        model = r.Read(path);
                        break;
                    }

                default:
                    throw new InvalidOperationException("File format not supported.");
            }

            return model;
        }
    }
}
